import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svprogresshud/flutter_svprogresshud.dart';
// import 'package:flutter_svprogresshud/flutter_svprogresshud.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';
import 'package:provider/provider.dart';

import '../Manager/gm_user_manager.dart';
import '../Model/gm_login_user.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final FocusNode focusNode = FocusNode();
  bool isFillPhone = false;

  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  void _phoneTextChanged(String value) {
    setState(() {
      isFillPhone = value.isNotEmpty && value.length == 11;
    });
  }

  Future<void> _loginApp() async {
    final username = _usernameController.text;
    final code = _codeController.text;

    if (username.isNotEmpty && code.isNotEmpty) {
      if (!username.isNotEmpty) {
        GMAppUtil.gmToast("请输入正确的手机号码");
        return;
      }

      if (code.length != 4) {
        GMAppUtil.gmToast("请输入正确验证码");
        return;
      }

      if (username == '10086996' && code == '1234') {

        FocusScope.of(context).unfocus();
        SVProgressHUD.show();
        GMLoginUser user = GMLoginUser(
            nick: "小星星",
            ids: "58799002",
            city: "深圳市",
            job: "市场推广",
            sex: "女",
            coins: 396,
            level: 1,
            sign: '一只狗带给人的最大快乐就是，当你对它装疯的时候，它不会取笑你，反而会跟你一起疯',
            icon:
                "https://c-ssl.dtstatic.com/uploads/blog/202304/08/20230408121103_b9ccb.thumb.1000_0.jpg",
            birthday: '2023-03-21');
        await GMUserManager().saveUser(user);
        // ignore: use_build_context_synchronously
        Future.delayed(const Duration(seconds: 2), () {
          SVProgressHUD.dismiss();
          Provider.of<GMUserManager>(context, listen: false).checkLoginStatus();
        });
      } else {
        GMAppUtil.gmToast("登录失败");
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      backgroundColor: const Color(0xffffffff),
      body: SafeArea(
        child: Column(
          children: [
            // 可滑动的页面
            Expanded(
              child: GestureDetector(
                onTap: () {
                  FocusScope.of(context).unfocus();
                },
                child: SingleChildScrollView(
                    child: SizedBox(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        padding: const EdgeInsets.fromLTRB(28, 36, 28, 0),
                        width: ScreenUtil().screenWidth,
                        // color: Colors.orange,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(
                              height: ScreenUtil().statusBarHeight + 44,
                            ),
                            const Text(
                              "手机快捷登录",
                              style: TextStyle(
                                  color: Color(0xff1a1a1a),
                                  fontSize: 24,
                                  fontWeight: FontWeight.w800),
                            ),
                            const SizedBox(
                              height: 12,
                            ),
                            const Text(
                              "未注册过的手机号将自动创建账号",
                              style: TextStyle(
                                  color: Color(0xFF666666), fontSize: 14),
                            ),
                            const SizedBox(
                              height: 70,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                const Text(
                                  "+86",
                                  style: TextStyle(
                                      color: Color(0xff1a1a1a),
                                      fontSize: 16,
                                      fontWeight: FontWeight.w500),
                                ),
                                const SizedBox(
                                  width: 16,
                                ),
                                Expanded(
                                  child: CupertinoTextField(
                                    cursorColor: const Color(0xff1a1a1a),
                                    maxLength: 11,
                                    onChanged: _phoneTextChanged,
                                    controller: _usernameController,
                                    placeholder: "请输入手机号",
                                    style: const TextStyle(
                                        color: Color(0xff1a1a1a),
                                        fontSize: 16,
                                        fontWeight: FontWeight.w500),
                                    placeholderStyle: const TextStyle(
                                        color: Color(0xFF666666), fontSize: 15),
                                    keyboardType: TextInputType.phone,
                                    decoration: const BoxDecoration(
                                        // color: Constants.viewBackgroudColor, // 设置背景颜色
                                        ),
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(
                              height: 12,
                            ),
                            const Divider(
                              height: 0.5,
                              color: Color(0xffcccccc),
                            ),
                            Container(
                              height: 0.5,
                              color: Colors.white,
                            ),
                            const SizedBox(
                              height: 32,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Expanded(
                                  child: CupertinoTextField(
                                    cursorColor: const Color(0xff1a1a1a),
                                    controller: _codeController,
                                    placeholder: "请输入密码",
                                    maxLength: 4,
                                    style: const TextStyle(
                                        color: Color(0xff1a1a1a),
                                        fontSize: 16,
                                        fontWeight: FontWeight.w500),
                                    placeholderStyle: const TextStyle(
                                        color: Color(0xFF666666), fontSize: 15),
                                    keyboardType: TextInputType.phone,
                                    decoration: const BoxDecoration(
                                        // color: Constants.viewBackgroudColor, // 设置背景颜色
                                        ),
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(
                              height: 12,
                            ),
                            const Divider(
                              height: 0.5,
                              color: Color(0xffcccccc),
                            ),
                            Container(
                              height: 0.5,
                              color: Colors.white,
                            ),
                            const SizedBox(
                              height: 50,
                            ),
                            InkWell(
                              onTap: () {
                                _loginApp();
                              },
                              child: Container(
                                height: 50,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(25),
                                    color: GMAppUtil.kAppThemColor),
                                child: const Center(
                                  child: Text(
                                    "登录",
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 15,
                                        fontWeight: FontWeight.w500),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
              ),
            ),
            // 底部固定高度的 Container
            Container(
              height: 50.0,
              padding: const EdgeInsets.fromLTRB(33, 0, 33, 16),
              child: InkWell(
                child: Center(
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text(
                        "点击登录及表示同意",
                        style:
                            TextStyle(color: Color(0xff444444), fontSize: 12),
                      ),
                      InkWell(
                        onTap: () => {GMAppUtil.gmShowTermServece(context, 1)},
                        child: const Text(
                          "《隐私政策》",
                          style: TextStyle(
                              color: GMAppUtil.kAppThemColor, fontSize: 12),
                        ),
                      ),
                      const Text(
                        "和",
                        style:
                            TextStyle(color: Color(0xff444444), fontSize: 12),
                      ),
                      InkWell(
                        onTap: () => {GMAppUtil.gmShowTermServece(context, 0)},
                        child: const Text(
                          "《用户协议》",
                          style: TextStyle(
                              color: GMAppUtil.kAppThemColor, fontSize: 12),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
